#include <QFileInfo>
#include "playlistmodel.h"

PlayListModel::PlayListModel(QObject *parent)
    :QAbstractListModel(parent)
{
    m_playList = new QMediaPlaylist();
}

bool PlayListModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent){
    return QAbstractListModel::dropMimeData(data,action,row,column,parent);
}

Qt::ItemFlags PlayListModel::flags(const QModelIndex &index) const
{
    return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsDragEnabled;
}

QModelIndex PlayListModel::index(int row, int column, const QModelIndex &parent) const
{
    return m_playList && !parent.isValid()
            && row >= 0 && row < m_playList->mediaCount()
        ? createIndex(row, column)
        : QModelIndex();
}

int PlayListModel::rowCount(const QModelIndex &parent) const
{
    return m_playList&&!parent.isValid()?m_playList->mediaCount():0;
}

 QVariant PlayListModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();
    if(role == Qt::DisplayRole){
        QUrl location = m_playList->media(index.row()).canonicalUrl();
        return QFileInfo(location.path()).fileName();
    }
    return QVariant();
}


void PlayListModel::setPlayList(QMediaPlaylist *playlist)
{
    beginResetModel();
    m_playList = playlist;
    endResetModel();
}
